package cn.iocoder.yudao.module.erp.controller.admin.customerinfo;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.module.erp.controller.admin.contractinfo.vo.ContractInfoRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategoryListReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.category.ErpProductCategoryRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductCategoryDO;
import cn.iocoder.yudao.module.erp.enums.DictTypeConstants;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

import cn.iocoder.yudao.module.erp.controller.admin.customerinfo.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.customerinfo.CustomerInfoDO;
import cn.iocoder.yudao.module.erp.service.customerinfo.CustomerInfoService;

@Tag(name = "管理后台 - 客户信息")
@RestController
@RequestMapping("/erp/customer-info")
@Validated
public class CustomerInfoController {

    @Resource
    private CustomerInfoService customerInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建客户信息")
    @PreAuthorize("@ss.hasPermission('erp:customer-info:create')")
    public CommonResult<Long> createCustomerInfo(@Valid @RequestBody CustomerInfoSaveReqVO createReqVO) {
        return success(customerInfoService.createCustomerInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户信息")
    @PreAuthorize("@ss.hasPermission('erp:customer-info:update')")
    public CommonResult<Boolean> updateCustomerInfo(@Valid @RequestBody CustomerInfoSaveReqVO updateReqVO) {
        customerInfoService.updateCustomerInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:customer-info:delete')")
    public CommonResult<Boolean> deleteCustomerInfo(@RequestParam("id") Long id) {
        customerInfoService.deleteCustomerInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:customer-info:query')")
    public CommonResult<CustomerInfoRespVO> getCustomerInfo(@RequestParam("id") Long id) {
        CustomerInfoDO customerInfo = customerInfoService.getCustomerInfo(id);
        return success(BeanUtils.toBean(customerInfo, CustomerInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户信息分页")
    @PreAuthorize("@ss.hasPermission('erp:customer-info:query')")
    public CommonResult<PageResult<CustomerInfoRespVO>> getCustomerInfoPage(@Valid CustomerInfoPageReqVO pageReqVO) {
        PageResult<CustomerInfoDO> pageResult = customerInfoService.getCustomerInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户信息 Excel")
    @PreAuthorize("@ss.hasPermission('erp:customer-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomerInfoExcel(@Valid CustomerInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerInfoDO> list = customerInfoService.getCustomerInfoPage(pageReqVO).getList();

        List<CustomerInfoRespVO> rlist=BeanUtils.toBean(list, CustomerInfoRespVO.class, customer -> {
            customer.setStatusName(DictFrameworkUtils.getDictDataLabel(DictTypeConstants.COMMON_STATUS, customer.getStatus()));
        });

        // 导出 Excel
        ExcelUtils.write(response, "客户信息.xls", "数据", CustomerInfoRespVO.class,
                rlist);
    }


    @GetMapping("/simple-list")
    @Operation(summary = "获得客户精简列表", description = "只包含被开启的客户，主要用于前端的下拉选项")
    public CommonResult<List<CustomerInfoSimpleVO>> getProductCategorySimpleList() {
        List<CustomerInfoDO> list = customerInfoService.getCustomerListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(convertList(list, customer -> new CustomerInfoSimpleVO().setId(customer.getId()).setName(customer.getName())));
    }

}